package com.walk.zworld.ui.activity

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.walk.zworld.R
import com.walk.zworld.mvvm.Constant
import com.walk.zworld.ui.fragment.SysKnowListFragment
import com.base.lib.base.view.BaseActivity
import com.base.lib.httpUtils.Knowledge
import com.base.lib.httpUtils.KnowledgeTreeBody

class KnowledgeActivity : BaseActivity() {

    private var data = mutableListOf<Knowledge>()
    private val viewPagerAdapter: KnowledgePagerAdapter by lazy {
        KnowledgePagerAdapter(this@KnowledgeActivity, data)
    }
    private lateinit var title: String
    private lateinit var vp2: ViewPager2
    private var pos: Int = 0

    override fun onBindLayout(): Int  = R.layout.activity_knowledge

    override fun initBundle() {
        intent?.extras?.let {
            title = it.getString(Constant.CONTENT_TITLE_KEY) ?: ""
            pos = it.getInt("postion")
            it.getSerializable(Constant.CONTENT_DATA_KEY)?.let { res ->
                val data1 = res as KnowledgeTreeBody
                data1.children.let { data.addAll(it) }
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView() {
        setTop(title)
        vp2 = findViewById(R.id.vp2_know)
        vp2.run {
            adapter = viewPagerAdapter
//            offscreenPageLimit = data.size
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {})
            currentItem = pos
        }
        val tl_know = findViewById<TabLayout>(R.id.tl_know)
        tl_know.run {
            addOnTabSelectedListener(onTabSelectedListener)
        }
        TabLayoutMediator(tl_know, vp2) { tab, position ->
            tab.text = viewPagerAdapter.list[position].name
        }.attach()
    }

    override fun initData() {
    }

    private val onTabSelectedListener = object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {}

        override fun onTabUnselected(tab: TabLayout.Tab?) {}

        override fun onTabSelected(tab: TabLayout.Tab?) {
            tab?.let { vp2.currentItem = it.position }
        }
    }

    class KnowledgePagerAdapter(val activity: FragmentActivity, val list: List<Knowledge>) :
        FragmentStateAdapter(activity) {

        private val fragments = mutableListOf<Fragment>()

        init {
            fragments.clear()
            list.forEach {
                fragments.add(SysKnowListFragment.newInstance(it.id))
            }
        }

        override fun getItemCount(): Int = list.size

        override fun createFragment(position: Int): Fragment {
            return SysKnowListFragment.newInstance(list[position].id)
        }
    }
}